package com.party.core.service.crowdfund.impl;

import com.party.common.constant.Constant;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.activity.Activity;
import com.party.core.model.activity.ActivityDetail;
import com.party.core.model.activity.BookOrderParameter;
import com.party.core.model.counterfoil.Counterfoil;
import com.party.core.model.counterfoil.CounterfoilBusiness;
import com.party.core.model.crowdfund.*;
import com.party.core.model.member.MemberActApplyParameter;
import com.party.core.model.order.OrderStatus;
import com.party.core.model.order.OrderType;
import com.party.core.service.activity.IActivityDetailService;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.counterfoil.ICounterfoilBusinessService;
import com.party.core.service.counterfoil.ICounterfoilService;
import com.party.core.service.crowdfund.*;
import com.party.core.service.member.IMemberActService;
import com.party.pay.model.query.TradeStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 众筹报名接口实现
 * Created by wei.li
 *
 * @date 2017/2/16 0016
 * @time 18:39
 */

@Service
public class ProjectApplyService implements IProjectApplyService {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityDetailService activityDetailService;

    @Autowired
    private IProjectContentService projectContentService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private ITargetProjectService targetProjectService;

    @Autowired
    private IAnalyzeService analyzeService;

    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private ICounterfoilService counterfoilService;

    @Autowired
    private ICounterfoilBusinessService counterfoilBusinessService;


    @Override
    @Transactional
    public String apply(ApplyParameter parameter, String counterfoilId) {
        Activity activity = activityService.get(parameter.getActivityId());
        //是否是众筹活动
        if (!activity.getIsCrowdfunded().equals(Constant.IS_CROWDFUNDED)) {
            throw new BusinessException("该活动不是众筹类型");
        }

        if (StringUtils.isEmpty(counterfoilId)) {
            throw new BusinessException("票据id不能为空");
        }
        Counterfoil counterfoil = counterfoilService.get(counterfoilId);
        if (null == counterfoil || null == counterfoil.getPayment()) {
            throw new BusinessException("票据不能为空");
        }

        // 订单下单
        BookOrderParameter orderParameter = new BookOrderParameter();
        orderParameter.setTitle(activity.getTitle());
        orderParameter.setInitiatorId(activity.getMember());
        orderParameter.setType(OrderType.ORDER_ACTIVITY.getCode());
        orderParameter.setStatus(OrderStatus.ORDER_STATUS_TO_BE_PAID.getCode());
        orderParameter.setActivityId(parameter.getActivityId());
        orderParameter.setUnit(1);
        orderParameter.setLinkman(parameter.getRealname());
        orderParameter.setPhone(parameter.getMobile());
        orderParameter.setMerchantId(TradeStatus.ZCXM.getCode());
        orderParameter.setMemberId(parameter.getMemberId());
        orderParameter.setPayment(counterfoil.getPayment());
        String orderId = activityService.bookOrder(orderParameter);

        // 保存报名关系
        MemberActApplyParameter memberActApplyParameter = new MemberActApplyParameter();
        memberActApplyParameter.setOrderId(orderId);
        memberActApplyParameter.setActivityId(parameter.getActivityId());
        memberActApplyParameter.setMemberId(parameter.getMemberId());
        memberActApplyParameter.setRealname(parameter.getRealname());
        memberActApplyParameter.setCompany(parameter.getCompany());
        memberActApplyParameter.setMobile(parameter.getMobile());
        memberActApplyParameter.setTitle(parameter.getTitle());
        memberActApplyParameter.setPayment(counterfoil.getPayment());
        memberActApplyParameter.setEndTime(activity.getEndTime());
        memberActService.apply(memberActApplyParameter, counterfoilId, activity.getMember());

        // 生成众筹
        ProjectApplyParameter projectApplyParameter = new ProjectApplyParameter();
        projectApplyParameter.setMemberId(parameter.getMemberId());
        projectApplyParameter.setActivityId(parameter.getActivityId());
        projectApplyParameter.setOrderId(orderId);
        projectApplyParameter.setDeclaration(parameter.getDeclaration());
        projectApplyParameter.setStyle(parameter.getStyle());
        projectApplyParameter.setProjectTitle(parameter.getProjectTitle());
        projectApplyParameter.setPayment(counterfoil.getPayment());
        String id = this.projectApply(projectApplyParameter, activity);

        //修改活动统计数

        activity.setBeCrowdfundNum(activity.getBeCrowdfundNum() + 1);
        activity.setJoinNum(activity.getJoinNum() + 1);
        activityService.update(activity);

        //维护多票据数据
        CounterfoilBusiness search = new CounterfoilBusiness();
        search.setBusinessId(id);
        search.setCounterfoilId(counterfoil.getId());
        search.setCreateBy(parameter.getMemberId());
        counterfoilBusinessService.insert(search);
        counterfoil.setJoinNum(counterfoil.getJoinNum() + 1);
        counterfoilService.update(counterfoil);
        return id;
    }

    /**
     * 众筹报名
     *
     * @param parameter 参数
     * @return 众筹编号
     */
    @Override
    public String projectApply(ProjectApplyParameter parameter, Activity activity) {
        Project project = new Project();
        project.setTitle(activity.getTitle());
        project.setTargetAmount(parameter.getPayment());
        project.setPic(activity.getPic());
        project.setEndDate(activity.getEndTime());
        project.setDeclaration(parameter.getDeclaration());
        project.setTitle(parameter.getProjectTitle());
        project.setStyle(parameter.getStyle());

        String content = null;
        //活动详情
        ActivityDetail activityDetail = activityDetailService.getByRefId(activity.getId());
        if (null != activityDetail && null != activityDetail.getContent()) {
            content = activityDetail.getContent();
        }
        String id = projectContentService.insert(new ProjectContent(content));
        project.setContentId(id);
        project.setAuthorId(parameter.getMemberId());
        String projectId = projectService.insert(project);

        //持久化众筹关联
        TargetProject targetProject = new TargetProject();
        targetProject.setTargetId(activity.getId());
        targetProject.setOrderId(parameter.getOrderId());
        targetProject.setProjectId(projectId);
        targetProject.setType(Constant.CROWD_FUND_TYPE_ACTIVITY);
        targetProject.setParentId(activity.getMember());
        targetProject.setDistributorId(parameter.getMemberId());
        targetProjectService.insert(targetProject);

        //统计分析
        Analyze analyze = new Analyze();
        analyze.setTargetId(projectId);
        analyzeService.insert(analyze);
        return project.getId();
    }
}
